package client;

import entities.Property;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import server.GameLogic;

/**
 *
 * @author casshern
 */
public class GameFrame extends JFrame {

    private WorldPanel worldPanel;
    private BottomPanel bottomPanel;
    private Client client;
    public static final int SHOW_GAME_INFO = 0;
    public static final int SHOW_OBJ_INFO = 1;
    private int infoType;

    public GameFrame(Client client) {
        this.client = client;
        this.setResizable(false);
        worldPanel = new WorldPanel(this.client);
        bottomPanel = new BottomPanel(this.client);
        worldPanel.setBottomPanel(bottomPanel);
        bottomPanel.setWorldPanel(worldPanel);
        infoType = SHOW_GAME_INFO;
        bottomPanel.showGameInfo();
        this.add(worldPanel, BorderLayout.NORTH);
        this.add(bottomPanel, BorderLayout.CENTER);
        this.setTitle("SpaceMiningXtreme");
        this.setVisible(true);
        this.setSize(1020, 650);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void gameUpdate(ArrayList<Property> properties) {
        if (properties.get(0).getPlayerID() != GameLogic.COMMON_INFO) {
            if (properties.get(0).getPlayerID() == GameLogic.WINNER1) {
                if (client.getID() == 1) {
                    JOptionPane.showMessageDialog(this, "You win!");
                } else {
                    JOptionPane.showMessageDialog(this, "You lose!");
                }
            } else if (properties.get(0).getPlayerID() == GameLogic.WINNER2) {
                if (client.getID() == 1) {
                    JOptionPane.showMessageDialog(this, "You lose!");
                } else {
                    JOptionPane.showMessageDialog(this, "You win!");
                }
            } else if (properties.get(0).getPlayerID() == GameLogic.DRAW) {
                JOptionPane.showMessageDialog(this, "Draw!");
            }
            System.exit(0);
        }
        worldPanel.gameUpdate(properties);
        bottomPanel.statusUpdate(properties.get(0));
        if (infoType == SHOW_GAME_INFO) {
            bottomPanel.showGameInfo();
        } else if (infoType == SHOW_OBJ_INFO) {
            worldPanel.showObjInfo();
        }
        String msg;
        if (client.getID() == 1) {
            msg = properties.get(0).getMsg1();
        } else {
            msg = properties.get(0).getMsg2();
        }
        if (msg != null) {
            bottomPanel.setMessage(msg);
        } else {
            bottomPanel.setMessage("Space Mining Xtreme");
        }
    }

    public void setInfoType(int infoType) {
        this.infoType = infoType;
    }
}
